package com.yanggeng.demo.beginnerguidedemo.utils;

import com.sun.mail.util.MailSSLSocketFactory;

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * 基于JavaMail发送邮件
 * Created by yanggeng on 2017/7/20.
 */

public class SendMailUtils {

    private static final String SERVICE = "smtp.qq.com";
    private static final String PORT = "465";
    private static final String FROM_USER = "aidenyang1994@qq.com";
    private static final String FROM_PWD = "issjgowhddbqdfge";
    private static final String TO = "aidenyang94@163.com";

    public static void send(final String subject, final String body, final String to, final List<String> pathList, final SendCallBack callBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                sendEmail(subject, body, to, pathList, callBack);
            }
        }).start();
    }



    private static void sendEmail(String subject, String body, String to, List<String> pathList, SendCallBack callBack) {
        Properties properties = new Properties();
        try {
            MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
            socketFactory.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", true);
            properties.put("mail.smtp.ssl.socketFactory", socketFactory);
            properties.put("mail.smtp.host", SERVICE);
            properties.put("mail.smtp.port", PORT);
            properties.put("mail.smtp.auth", true);
            Transport transport = null;
            Session session = Session.getDefaultInstance(properties, null);
            MimeMessage message = new MimeMessage(session);
            transport = session.getTransport("smtp");
            transport.connect(SERVICE, FROM_USER, FROM_PWD);
            message.setSentDate(new Date());
            InternetAddress fromAddress = new InternetAddress(FROM_USER);
            message.setFrom(fromAddress);
            InternetAddress[] toAddress = new InternetAddress[1];
            toAddress[0] =new InternetAddress(to);
            message.setRecipients(Message.RecipientType.TO, toAddress);
            message.setSubject(subject, "UTF-8");
            MimeMultipart multi = new MimeMultipart(); //代表整个邮件
            BodyPart bodyPart = new MimeBodyPart(); //正文对象
            bodyPart.setText(body);  //设置正文
            multi.addBodyPart(bodyPart); //添加正文到邮件
            if (pathList != null) {
                for (String path : pathList) {
                    if (!new File(path).exists()) {
                        continue;
                    }
                    FileDataSource fds = new FileDataSource(path);
                    BodyPart fileBodyPart = new MimeBodyPart();
                    fileBodyPart.setDataHandler(new DataHandler(fds));
                    String fileName = MimeUtility.encodeText(fds.getName(), "UTF-8", null);
                    fileBodyPart.setFileName(fileName);
                    multi.addBodyPart(fileBodyPart);
                }
            }
            message.setContent(multi);
            message.saveChanges();
            transport.sendMessage(message, message.getAllRecipients()); //发送邮件
            transport.close();
            callBack.sendBack(true);
        } catch (Exception e) {
            callBack.sendBack(false);
            e.printStackTrace();
        }
    }

    public interface SendCallBack {
        void sendBack(boolean isSuccess);
    }

}
